package nccloud.impl.bd.datacoll;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import nc.bs.bd.baseservice.md.BatchBaseService;
import nc.bs.dao.BaseDAO;
import nc.bs.datacoll.bp.ExlFileBp;
import nc.bs.datacoll.bp.QueryBp;
import nc.bs.datacoll.bp.SaveBp;
import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Logger;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.md.persist.framework.IMDPersistenceQueryService;
import nc.ui.querytemplate.querytree.IQueryScheme;
import nc.vo.bd.meta.BatchOperateVO;
import nc.vo.hrjx.DataCollVO;
import nc.vo.pub.BusinessException;
import nccloud.itf.bd.datacoll.IDatacollQryService;
import nccloud.itf.bd.datacoll.IDatacollService;

/**
 * <b> </b>
 *
 * <p>
 *
 * </p>
 *
 * Create at 2009-12-25 上午09:31:57
 *
 * @author bq
 * @since V6.0
 */

public class DatacollServiceImpl extends BatchBaseService<DataCollVO> implements IDatacollService, IDatacollQryService {

	private IMDPersistenceQueryService queryService = null;

	public DatacollServiceImpl() {
		super("b9a29cf8-9344-4332-b5e5-351bf1351b9e");
	}

	@Override
	public BatchOperateVO batchSaveCurrtype(BatchOperateVO vo) throws BusinessException {
		return batchSave(vo);
	}

	@Override
	public DataCollVO findByPK(String key) throws BusinessException {
		DataCollVO dataCollVO = null;
		try {
			Object vo = new BaseDAO().retrieveByPK(DataCollVO.class, key);
			dataCollVO = (vo == null ? null : (DataCollVO) vo);
		} catch (Exception e) {
			Logger.error(e.getMessage(), e);
			throw new BusinessException(e);
		}

		return dataCollVO;
	}

	@SuppressWarnings("unchecked")
	@Override
	public DataCollVO[] queryAll() throws BusinessException {
		Collection<DataCollVO> col = getQueryService().queryBillOfVOByCondWithOrder(DataCollVO.class, "1=1", true,
				false, new String[] {});
		return col.toArray(new DataCollVO[0]);
	}

	@SuppressWarnings("unchecked")
	@Override
	public DataCollVO[] query(String where) throws BusinessException {
		Collection<DataCollVO> col = getQueryService().queryBillOfVOByCondWithOrder(DataCollVO.class, where, true,
				false, new String[] {});

		return col.toArray(new DataCollVO[0]);
	}

	private IMDPersistenceQueryService getQueryService() {
		if (queryService == null)
			queryService = NCLocator.getInstance().lookup(IMDPersistenceQueryService.class);
		return queryService;

	}

	@Override
	public String[] queryPKs(String where) throws BusinessException {
		String[] pks = null;
		StringBuilder querySQL = new StringBuilder("select ");
		querySQL.append(" pk_datacoll ");
		querySQL.append(" from ");
		querySQL.append(" hrjx_datacoll ");
		querySQL.append(" where ");
		querySQL.append(where);
		querySQL.append(" order by ");
		querySQL.append(" ts ");

		List<String> pkList = (ArrayList<String>) new BaseDAO().executeQuery(querySQL.toString(),
				new ColumnListProcessor());
		if (pkList != null && pkList.size() > 0) {
			pks = pkList.toArray(new String[pkList.size()]);
		}

		return pks;
	}

	@Override
	public DataCollVO[] query(String sessionid, IQueryScheme scheme) throws BusinessException {
		return new QueryBp().exec(sessionid, scheme);
	}

	@Override
	public BatchOperateVO batchSave(String sessionid, BatchOperateVO vo) throws BusinessException {
		return new SaveBp().exec(sessionid, vo);
	}

	@Override
	public File exportExlFile(String sessionid, DataCollVO[] vos) throws Exception {
		return new ExlFileBp().exec_exp(sessionid, vos);
	}

	@Override
	public String importExlFile(String sessionid, InputStream input, String fileName) throws Exception {
		return new ExlFileBp().exec_imp(sessionid, input, fileName);
	}

	@Override
	public Map<String, Object> queryByPage(String sessionid, IQueryScheme scheme, int pagecount, int pageNum)
			throws BusinessException {
		return new QueryBp().execByPage(sessionid, scheme, pagecount, pageNum);
	}

	@Override
	public Map<String, Object> queryByPage(String sessionid, Map<String, Object> querycondition, int pagecount,
			int pageNum) throws BusinessException {
		return new QueryBp().execByPage(sessionid, querycondition, pagecount, pageNum);
	}
}
